package cn.com.thinker.core.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
@ConditionalOnClass(DruidDataSource.class)
@ConditionalOnProperty(name="spring.datasource.druid.enable", havingValue="true")
@ConfigurationProperties(prefix="spring.datasource")
public class DruidConfig {
	
	private String url;
	private String username;
	private String password;
	private String driverClassName;
	private String publicKey;
	
	@Bean
    public ServletRegistrationBean druidServlet() {
        return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    }
	
	@Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }
	
	@Bean
    public DataSource druidDataSource() {
		
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        try {
            druidDataSource.setFilters("stat,wall,config");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        druidDataSource.setConnectionProperties(String.format("config.decrypt=true;config.decrypt.key=%s",publicKey));
        return druidDataSource;
    }

	public void setUrl(String url) {
		this.url = url;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}
	
	public static void main(String[] args) throws Exception {
		String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKTZNMtZpzun55uJq3q3A9UYM2//LvDLCXpbhaK2y8SkPKHo/qQt0N4PqWyvsxaA6TQRvdTpW0no3d7zvAMN8H0CAwEAAQ==";
		String password = "XssB3UZMyaVwt9N0cq7y8kjRk37Nv19rP2tyFZAHfoJryeaeK4JRVK6kWnINBWGdxSBH2GkjKvlVlri5b2eHsg==";
		String result = com.alibaba.druid.filter.config.ConfigTools.decrypt(publicKey, password);
		System.out.println(result);
	}
	
}
